/*
 * mqtt.c
 *
 *  Created on: 2017
 *      Author: zjm
 */

#include "../mongoose.h"
#include "../json/json.h"
#include "../b64/base64.h"
#include "db.h"


int get_mqtt_server(struct mg_connection *nc)
{
	dbFile db;
	char *rest = "undefined";
	db_open("/data/data.db",&db);

	dbStart(&db);

	dbSql(&db,"select server from t_config;");

	if(dbStep(&db) == SQLITE_ROW){
		rest = dbGetResultString(&db,0);
	}

	mg_printf_html_escape(nc,"%s",rest);

	dbDone(&db);

	db_close(&db);

	return 0;
}

int get_mqtt_port(struct mg_connection *nc)
{
	dbFile db;
	char *rest = "undefined";
	db_open("/data/data.db",&db);

	dbStart(&db);

	dbSql(&db,"select port from t_config;");

	if(dbStep(&db) == SQLITE_ROW){
		rest = dbGetResultString(&db,0);
	}

	mg_printf_html_escape(nc,"%s",rest);

	dbDone(&db);

	db_close(&db);

	return 0;
}

int get_mqtt_username(struct mg_connection *nc)
{
	dbFile db;
	char *rest = "undefined";
	db_open("/data/data.db",&db);

	dbStart(&db);

	dbSql(&db,"select username from t_config;");

	if(dbStep(&db) == SQLITE_ROW){
		rest = dbGetResultString(&db,0);
	}

	mg_printf_html_escape(nc,"%s",rest);

	dbDone(&db);

	db_close(&db);

	return 0;
}

int get_mqtt_passwd(struct mg_connection *nc)
{
	dbFile db;
	char *rest = "undefined";
	db_open("/data/data.db",&db);

	dbStart(&db);

	dbSql(&db,"select passwd from t_config;");

	if(dbStep(&db) == SQLITE_ROW){
		rest = dbGetResultString(&db,0);
	}

	mg_printf_html_escape(nc,"%s",rest);

	dbDone(&db);

	db_close(&db);

	return 0;
}

int mqtt_save(struct mg_connection *nc,struct http_message *hm)
{
	char var[512];
	struct json *J;
	int error;
	dbFile db;

	if(mg_url_decode(hm->query_string.p,hm->query_string.len,var,512,0) > 0){

		J = json_open(JSON_F_NONE, &error);

		error = json_loadbuffer(J,var,strlen(var));
		if(error == 0){
			const char *id = json_string(J,"clientID");
			const char *server = json_string(J,"server");
			int port = json_number(J,"port");
			const char *user = json_string(J,"user");
			const char *password = json_string(J,"password");
			const char *name = json_string(J,"name");
			time_t time = json_number(J,"time");

			if(server && *server){
				db_open("/data/data.db",&db);

				dbStart(&db);

				char name_b64[256];

				memset(name_b64,0,sizeof(name_b64));
				base64_decode(name,strlen(name),name_b64);

				snprintf(var,sizeof var,
						"update [t_config] set server='%s',port=%d,username='%s',passwd='%s',id='%s',name=? where rowid=1 ;",
						server,port,user,password,id);

				dbSql(&db,var);

				sqlite3_bind_blob(db.stmt,1,name_b64,strlen(name_b64),NULL);

				error = dbStep(&db);
				if(error != SQLITE_DONE){
					printf("sql error:%s\n",sqlite3_errmsg(db.db));
				}

				dbDone(&db);

				db_close(&db);
			}

			if(time){
				char cmd[256];

				snprintf(cmd,sizeof cmd,"date @%lu;hwclock -w",(time+3600*8));
				system(cmd);
			}
		}
	}

	mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");

	mg_printf_http_chunk(nc,"{\"status\":\"ok\"}");

	mg_send_http_chunk(nc, "", 0);

	return 0;
}

int mqtt_get(struct mg_connection *nc,struct http_message *hm)
{
	struct json *J;
	int error;
	dbFile db;
	char buffer[256];

	db_open("/data/data.db",&db);

	dbStart(&db);

	dbSql(&db,"select * from t_config;");

	J = json_open(JSON_F_NONE, &error);
	while(dbStep(&db) == SQLITE_ROW){
		int column = db_column_count(&db);
		for (int i = 0; i < column; ++i) {
			const char *header = db_column_name(&db,i);
			int type = db_column_type(&db,i);
			if(!strcmp(header,"server")){
				const char *value = dbGetResultString(&db,i);
				json_setstring(J,value,"server");
			}else if(!strcmp(header,"port")){
				int value = dbGetResultInt(&db,i);
				json_setnumber(J,value,"port");
			}else if(!strcmp(header,"username")){
				const char *value = dbGetResultString(&db,i);
				json_setstring(J,value,"user");
			}else if(!strcmp(header,"passwd")){
				const char *value = dbGetResultString(&db,i);
				json_setstring(J,value,"password");
			}else if(!strcmp(header,"id")){
				const char *value = dbGetResultString(&db,i);
				json_setstring(J,value,"clientID");
			}else if(!strcmp(header,"name")){
//				const char *value = dbGetResultString(&db,i);
				int len = sqlite3_column_bytes(db.stmt,i);
				uint8_t *dp = sqlite3_column_blob(db.stmt,i);

				char name_b64[256];
				memset(name_b64,0,sizeof(name_b64));
				base64_encode(dp,len,name_b64);
				json_setstring(J,name_b64,"name");
			}else if(!strcmp(header,"Qos")){
				int value = dbGetResultInt(&db,i);
				json_setnumber(J,value,"qos");
			}
		}
	}

	json_printstring(J,buffer,sizeof(buffer),JSON_F_NONE,&error);

//	json_printfile(J,stdout,JSON_F_NONE);

	json_close(J);

	mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");

	mg_printf_http_chunk(nc,buffer);

	mg_send_http_chunk(nc, "", 0);

	dbDone(&db);

	db_close(&db);

	return 0;
}
